package com.dykj.fireFighting.web.fireNotice;

import com.dykj.fireFighting.api.socialUnit.fireNotice.dto.FireNoticeDto;
import com.dykj.fireFighting.api.socialUnit.fireNotice.entity.FireNotice;
import com.dykj.fireFighting.api.socialUnit.fireNotice.service.IFireNoticeService;
import com.dykj.fireFighting.api.socialUnit.fireNotice.vo.FireNoticeVo;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.utils.UUIDUtils;
import com.dykj.fireFighting.common.core.vo.LoginUserVo;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Date;


/**
 * 消防公告接口
 *
 * @Author hxb
 * @Date 2021-06-23 08:58:01
 */
@Slf4j
@Api(value = "消防公告接口", tags = {"消防公告接口"})
@RestController
@RequestMapping("/fireNotice")
public class FireNoticeController {

    @DubboReference
    private IFireNoticeService fireNoticeService;


    /**
     * 消防公告列表
     *
     * @return
     */
    @AutoLog(value = "消防公告-分页列表查询")
    @ApiOperation(value = "消防公告分页列表", notes = "消防公告分页列表")
    @GetMapping("/list")
    @RequiresPermissions("socialUnit/fireNotice/list")
    public CommonResult<PageVo<FireNoticeVo>> treeGrid(FireNoticeDto fireNoticeDto) {
        LoginUserVo user = ShiroUtil.getUser();
        fireNoticeDto.setUnitId(user.getEnterpriseId().toString());
        PageVo<FireNoticeVo> page = fireNoticeService.pageList(fireNoticeDto);
        return CommonResult.success(page);
    }

    /**
     * 获取消防公告信息
     */
    @AutoLog(value = "消防公告-获取消防公告信息")
    @ApiOperation(value = "获取消防公告信息", notes = "获取消防公告信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
    @RequiresPermissions("socialUnit/fireNotice/info")
    public CommonResult<FireNotice> info(@PathVariable("id") String id) {
        FireNotice fireNotice = fireNoticeService.getById(id);
        return CommonResult.success(fireNotice);
    }

    /**
     * 保存消防公告信息
     */
    @AutoLog(value = "消防公告-保存消防公告信息")
    @ApiOperation(value = "保存消防公告信息", notes = "保存消防公告信息")
    @PostMapping("/save")
    @RequiresPermissions("socialUnit/fireNotice/save")
    public CommonResult save(@Valid @RequestBody FireNotice fireNotice) {
        try {
            Document document = Jsoup.parse(fireNotice.getContent());
            String content = document.text();
            LoginUserVo user = ShiroUtil.getUser();
            fireNotice.setId(UUIDUtils.get32UUID());
            fireNotice.setContent(content);
            fireNotice.setUnitId(user.getEnterpriseId().toString());
            fireNotice.setCreateTime(new Date());
            fireNotice.setCreateBy(Long.toString(user.getId()));
            fireNoticeService.save(fireNotice);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 修改消防公告信息
     */
    @AutoLog(value = "消防公告-修改消防公告信息")
    @ApiOperation(value = "修改消防公告信息", notes = "修改消防公告信息")
    @PostMapping("/update")
    @RequiresPermissions("socialUnit/fireNotice/update")
    public CommonResult update(@Valid @RequestBody FireNotice fireNotice) {
        try {
            Document document = Jsoup.parse(fireNotice.getContent());
            String content = document.text();
            fireNotice.setContent(content);
            Long userId = ShiroUtil.getUser().getId();
            fireNotice.setUpdateTime(new Date());
            fireNotice.setUpdateBy(Long.toString(userId));
            fireNoticeService.updateById(fireNotice);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 删除消防公告信息
     */
    @AutoLog(value = "消防公告-删除消防公告信息")
    @ApiOperation(value = "删除消防公告信息", notes = "删除消防公告信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "String", required = true, allowMultiple = true)
    @PostMapping("/delete")
    @RequiresPermissions("socialUnit/fireNotice/delete")
    public CommonResult delete(@RequestBody String[] ids) {
        try {
            fireNoticeService.updateByIds(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }
}
